package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.projectFileInformation;
import com.qiyeji.real_estate.entity.projectFileInformationKey;
import com.qiyeji.real_estate.service.ProjectFileInformationService;
import com.qiyeji.real_estate.utils.Result;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RequestMapping("/ProjectFileInformation")
@RestController
@ResponseBody
public class ProjectFileInformationController {
    @Autowired
    ProjectFileInformationService projectFileInformationService;

    @PostMapping("/getAllProjectFileInformation")
    public Result getAllProjectFileInformation() {
        List<projectFileInformation> projectFileInformation = projectFileInformationService.getAllProjectFileInformation();
        if(projectFileInformation==null) {
            return Result.error();
        }
        else {
            return Result.success(projectFileInformation);
        }
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody projectFileInformation projectFileInformation){
        int res=projectFileInformationService.insert(projectFileInformation);
        if(res==0) {
            return Result.error();
        }
        else {
            return Result.success(res);
        }
    }

    @PostMapping("/deleteFolder")
    public Result delete(@RequestBody Map<String,String> params){
        projectFileInformation projectFileInformation=projectFileInformationService.selectByPrimaryKey(params.get("pCode"));
        String path=projectFileInformation.getPath();
        File file=new File(path);
        boolean flag=false;
        try {
            FileUtils.forceDelete(file);
            flag=true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(!flag){
            return Result.error();
        }else{
            return Result.success(flag);
        }
    }



    @PostMapping("/update")
    public Result update(@RequestBody projectFileInformation projectFileInformation){
        int res=projectFileInformationService.updateByPrimaryKey(projectFileInformation);
        if(res==0){
            return Result.error();
        }else{
            return Result.success(res);
        }
    }

    @PostMapping("/selectById")
    public Result selectById(@RequestBody Map<String,String> params){
        projectFileInformation projectFileInformation=projectFileInformationService.selectByPrimaryKey(params.get("pCode"));
        if(projectFileInformation==null){
            return Result.error();
        }else{
            return Result.success(projectFileInformation);
        }
    }

    @PostMapping("/fuzzyQuery")
    public Result fuzzyQuery(@RequestBody Map<String,String> params){
        String key = params.get("key");
        List<projectFileInformation> projectFileInformation = projectFileInformationService.fuzzyQuery(key);
        if(projectFileInformation==null) {
            return Result.error();
        }
        else {
            return Result.success(projectFileInformation);
        }
    }

    @PostMapping("/mkdirFile")
    public void mkdirFile(@RequestBody Map<String,String> params){
        String fileName=params.get("fileName");
        System.out.println("fileName:"+fileName);
        projectFileInformationService.mkdirFile(fileName);
    }

    @PostMapping("/getAllFileName")
    public Result getAllFileName(){
        List<String> list=projectFileInformationService.getAllFileName();
        if(list==null) {
            return Result.error();
        }
        else {
            return Result.success(list);
        }
    }

    @PostMapping("/getUncreated")
    public Result getUncreated(){
        List<String> list=projectFileInformationService.getUncreated();
        if(list==null) {
            return Result.error();
        }
        else {
            return Result.success(list);
        }
    }

    @PostMapping("/getFileNameBysCode")
    public Result getFileNameBysCode(@RequestBody Map<String,String> params){
        List<projectFileInformation> projectFileInformation=projectFileInformationService.getFileNameBysCode(params.get("sCode"));
        if(projectFileInformation==null) {
            return Result.error();
        }
        else {
            return Result.success(projectFileInformation);
        }
    }
}
